package edu.gmu.projects.fireresponder.comm;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TextEncryptor {

	private static TextEncryptor singleton;
	
	private Cipher encryptionCipher;
	private Cipher decryptionCipher;

	static {
		singleton = new TextEncryptor();
	}
	
	public static TextEncryptor getTextEncryptor() {
		return singleton;
	}
	
	public Cipher getEncryptionCipher() {
		return encryptionCipher;
	}

	public Cipher getDecryptionCipher() {
		return decryptionCipher;
	}
	
	private TextEncryptor() {
		byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
				0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
		byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
				0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
		SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

		try {
			encryptionCipher = Cipher.getInstance("AES/CFB8/PKCS5Padding", "BC");
			decryptionCipher = Cipher.getInstance("AES/CFB8/PKCS5Padding", "BC");
			IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

			encryptionCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);		
			decryptionCipher.init(Cipher.DECRYPT_MODE, key, ivSpec);		
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		}
	}
	
	public String encryptText(String plainText) {
		byte[] input = plainText.getBytes();
		byte[] cipherText = new byte[input.length];
		try {
			cipherText = encryptionCipher.doFinal(input);
			return new String(cipherText);
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String decryptText(String encryptedText) {
		byte[] input = encryptedText.getBytes();
		byte[] plainText = new byte[input.length];
		try {
			plainText = decryptionCipher.doFinal(input);
			return new String(plainText);
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		TextEncryptor encryptor = getTextEncryptor();
		String msg  = "this is my message";
		String encrypted = encryptor.encryptText(msg);
		System.out.println(encrypted);
		System.out.println(encryptor.decryptText(encrypted));
	}
}